package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"likeadmin/client/schemas/req"
	"likeadmin/client/service/user"
	"likeadmin/core"
	"likeadmin/core/response"
	"likeadmin/util"
)

var ListeningGroup = core.Group("/listening", newHandler, listening)

func newHandler(srv user.ISListeningService) *listeningHandler {
	return &listeningHandler{srv: srv}
}

func listening(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *listeningHandler) {
		// /pages/eat/index?source_id=x0001
		// https://listening.eeopn.com/client/listening/oppo?os=$os$&os_version=$ov$&model=$m$&lang=$lan$&country=$c$&width=$w$&height=$h$&pkg=$pkg$&app_version=$av$&useragent=$ua$&referer=$rf$&ip=__IP__&net_type=$nt$&carrier=$ca$&progress=$progress$&imei=$im$&oaid=__OAID__&ad_id=$ad$&ad_name=$an$&req_id=$req$&spread_id=x0001

		rg.Any("/oppo", handle.oppo)
		rg.Any("/oceanengine", handle.oceanengine)
		rg.GET("/oppo_test", handle.oppoTest)
	})
}

type listeningHandler struct {
	srv user.ISListeningService
}

func (lh listeningHandler) oppo(c *gin.Context) {
	var loginReq req.ListeningOppoReq

	// 兼容GET和POST请求参数
	if c.Request.Method == "GET" {
		// 处理POST请求的JSON数据
		if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &loginReq)) {
			return
		}
		// 可根据需要映射其他参数到req结构体字段
	} else {
		// 处理POST请求的JSON数据
		if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &loginReq)) {
			return
		}
	}

	res, err := lh.srv.Oppo(c, &loginReq)

	response.CheckAndRespWithData(c, res, err)
}

func (lh listeningHandler) oceanengine(c *gin.Context) {
	var loginReq req.ListeningOceanengineReq

	// 兼容GET和POST请求参数
	if c.Request.Method == "GET" {
		// 处理POST请求的JSON数据
		if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &loginReq)) {
			return
		}
		// 可根据需要映射其他参数到req结构体字段
	} else {
		// 处理POST请求的JSON数据
		if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &loginReq)) {
			return
		}
	}

	res, err := lh.srv.Oceanengine(c, &loginReq)

	response.CheckAndRespWithData(c, res, err)
}

func (lh listeningHandler) oppoTest(c *gin.Context) {
	var oppoTestReq req.OppoTestReq

	fmt.Printf("id:%s\n", oppoTestReq.Id)

	// 处理POST请求的JSON数据
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &oppoTestReq)) {
		return
	}

	res, err := lh.srv.OppoTest(c, &oppoTestReq)

	response.CheckAndRespWithData(c, res, err)
}
